package von.seiji.cn;

import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.symmetric.AES;
import org.junit.Test;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.stream.IntStream;

public class fileT {

    //MB
    final int MIN_SIZE = 10;

    @Test
    public void encrypt() {
        String path = "C:\\Users\\Administrator\\Desktop\\temp\\pic\\51792768_da2-1-64000000-000159.mp4";
        byte[] bytes = FileUtil.readBytes(path);
        AES aes = SecureUtil.aes("SeiJi1234567890-".getBytes(StandardCharsets.UTF_8));
        byte[] encrypt = aes.encrypt(bytes);
        FileUtil.writeBytes(encrypt, "C:\\Users\\Administrator\\Desktop\\temp\\pic\\OUT\\miss.z");
    }

    @Test
    public void decrypt() throws FileNotFoundException {
        String path = "C:\\Users\\Administrator\\Desktop\\temp\\pic\\OUT\\miss.z";
        AES aes = SecureUtil.aes("SeiJi1234567890-".getBytes(StandardCharsets.UTF_8));
        byte[] decrypt = aes.decrypt(new FileInputStream(path));
        FileUtil.writeBytes(decrypt, "C:\\Users\\Administrator\\Desktop\\temp\\pic\\OUT\\miss.x");

    }


    @Test
    public void kldsaf() throws IOException {
        String path = "C:\\Users\\Administrator\\Desktop\\temp\\pic\\51792768_da2-1-64000000-000159.mp4";
        String out = "C:\\Users\\Administrator\\Desktop\\temp\\pic\\OUT\\";
        crop(new File(path), FileUtil.file(out));

    }

    public void crop(File source, File directory) throws IOException {
        System.out.println("file name:" + source.getName());
        FileInputStream stream = new FileInputStream(source);
        DataInputStream dis = new DataInputStream(stream);
        byte[] bytes = new byte[MIN_SIZE * 1024 * 1000];
        File outDirectory = new File(directory.getAbsolutePath(), FileUtil.mainName(source));
        if (!outDirectory.exists() && !outDirectory.mkdirs()) {
            throw new RuntimeException("create directory[" + outDirectory + "] occur error");
        }

        int available = dis.available();
        System.out.println("file size: " + available);
        int times = available % bytes.length == 0 ? available / bytes.length : available / bytes.length + 1;
        System.out.println("expected times: " + times);
        System.out.println("-------------------------------------start-------------------------------------");
        long current = System.currentTimeMillis();
        System.out.println("current time:" + new Date(current));
        IntStream.range(0, times).forEach(item -> {
            System.out.println("clip file at " + (item + 1));
            File file = new File(outDirectory, (item + 1) + "." + FileUtil.extName(source));
            System.out.println("storage file:" + file.getAbsolutePath());

            FileOutputStream os = null;
            DataOutputStream dos = null;
            try {
                int read = dis.read(bytes);
                System.out.println("consumer input stream size:" + read);
                System.out.println("remain size of input stream:" + dis.available());
                os = new FileOutputStream(file);
                dos = new DataOutputStream(os);
                dos.write(bytes, 0, read);
                dos.flush();
            } catch (IOException e) {
            } finally {
                try {
                    System.out.println("i see if it work");
                    if (stream.available() == 0 || (~item ^ -times) == 0) {
                        System.out.println("oh, that maybe right");
                        dos.close();
                        os.close();
                        dis.close();
                        stream.close();
                    }
                } catch (IOException e) {
                }
            }

        });
        System.out.println("-------------------------------------end-------------------------------------");
        System.out.println("consume time:" + (System.currentTimeMillis() - current));
    }

    @Test
    public void ldaf() throws FileNotFoundException {

        String path = "C:\\Users\\Administrator\\Desktop\\temp\\pic\\OUT\\51792768_da2-1-64000000-000159.mp4";
        String out = "C:\\Users\\Administrator\\Desktop\\temp\\pic\\OUT\\51792768_da2-1-64000000-000159";
        merge(new File(out), FileUtil.file(path));
    }


    public void merge(File directory, File dest) throws FileNotFoundException {
        File[] files = directory.listFiles();
        System.out.println("file size:" + files.length);
        if (!dest.getParentFile().exists() && !dest.getParentFile().mkdirs()) {
            throw new RuntimeException("create directory occur error");
        }
        String ext = FileUtil.extName(dest);
        FileOutputStream fos = new FileOutputStream(dest);
        DataOutputStream dos = new DataOutputStream(fos);
        byte[] bytes = new byte[MIN_SIZE * 1024 * 1000];
        System.out.println("-------------------------------------start-------------------------------------");
        long current = System.currentTimeMillis();
        System.out.println("current time:" + new Date(current));
        Arrays.stream(files).filter(item -> ext.equals(FileUtil.extName(item)))
                .sorted(Comparator.comparing(item -> FileUtil.mainName(item)))
                .forEach(item -> {
                    System.out.println("current file name:" + item.getName());
                    FileInputStream stream = null;
                    try {
                        stream = new FileInputStream(item);
                        DataInputStream dis = new DataInputStream(stream);
                        int read = dis.read(bytes);
                        dos.write(bytes, 0, read);
                        dos.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                });
        System.out.println("-------------------------------------end-------------------------------------");
        System.out.println("consume time:" + (System.currentTimeMillis() - current));
    }
}
